package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.PopupFactory;

import model.Location;
import model.Stack;
import service.Service;

public class StackPanel extends JPanel {

    private Location location;
    private InfoPanel popupPanel;
    private PopupFactory pf = PopupFactory.getSharedInstance();
    private Popup popup;
    JLabel lbl = new JLabel();

    public StackPanel(Location location) {
	this.location = location;

	setLayout(null);
	addMouseListener(new MyMouseListener());
	setSize(75, 75);
	if (!location.isActive()) {
	    lbl.setText("Disabled");
	} else if (location.getStack() == null) {
	    lbl.setText("free");
	} else {
	    lbl.setText(("<html>" + location.getStack().getProductType()
		    .getName())
		    + "<br>"
		    + Service.getInstance().milisToTime(
			    (long) location.getStack().getCurrentProcess()
				    .calculateTimeLeft()) + "</html>");
	}
	add(lbl);
	lbl.setLocation(5, 5);
	lbl.setSize(65, 65);
	if (location.isActive()) {
	    setBackground(new Color(0x00, 0xFF, 0x00));
	}
    }

    @Override
    public void paintComponent(Graphics g) {
	super.paintComponent(g);
	g.setColor(Color.black);
	g.drawRect(0, 0, 74, 74);
    }

    public void updateView() {
	if (!location.isActive()) {
	    lbl.setText("Disabled");
	    setBackground(new Color(0x9E, 0x9E, 0x9E));
	} else if (location.getStack() == null) {
	    lbl.setText("free");
	    setBackground(new Color(0xFF, 0xFF, 0xFF));
	} else {
	    lbl.setText(("<html>" + location.getStack().getProductType()
		    .getName())
		    + "<br>"
		    + Service.getInstance().milisToTime(
			    (long) location.getStack().getCurrentProcess()
				    .calculateTimeLeft()) + "</html>");
	    int sec = (int) location.getStack().getCurrentProcess()
		    .calculateTimeLeft() / 1000;

	    int maxTime = (int) (location.getStack().getCurrentProcess()
		    .getProcess().getMaxTime() / 1000);

	    if (sec > 0) {
		double nom = (double) maxTime / 510;
		int current = (int) (sec / nom);

		if (current > 255) {
		    setBackground(new Color(2 * 0xFF - current, 0xFF, 0x00));
		} else {
		    setBackground(new Color(0xFF, current, 0x00));
		}
	    }

	}
    }

    public Location getStackLocation() {
	return location;
    }

    private class MyMouseListener implements MouseListener {

	@Override
	public void mouseClicked(MouseEvent arg0) {
	    Stack stack = StackPanel.this.location.getStack();
	    if (stack != null) {
		StackInfoDialog info = new StackInfoDialog(stack,
			StackPanel.this);
		info.setVisible(true);
	    } else {
		EditLocation el = new EditLocation(StackPanel.this);
	    }

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	    Stack stack = StackPanel.this.location.getStack();
	    if (stack == null) {
		popupPanel = new InfoPanel("The location does no have a stack");
	    } else {
		popupPanel = new InfoPanel("<html>"
			+ "Name: "
			+ stack.getProductType().getName()
			+ "<br>"
			+ "Current process: "
			+ stack.getCurrentProcess().getProcess().getName()
			+ "<br>"
			+ "Next process: "
			+ (stack.getNextProcess() == null ? "none" : stack
				.getNextProcess().getProcess().getName())
			+ "<br>"
			+ "Start time: "
			+ Service.getInstance().displayTime(
				stack.getCurrentProcess().getStartTime()
					.getTime()) + "</html>");
	    }

	    JButton button = new JButton("Hello, World");
	    int x, y; // right bottom corner of a stack

	    x = (int) arg0.getComponent().getX()
		    + arg0.getComponent().getWidth();

	    if (x + 200 >= arg0.getComponent().getParent().getWidth()) {
		x = x - 275;
	    }

	    y = (int) arg0.getComponent().getY()
		    + arg0.getComponent().getHeight();

	    if (y + 100 >= arg0.getComponent().getParent().getHeight()) {
		y = y - 175;
	    }

	    popup = pf.getPopup(StackPanel.this, popupPanel, (int) arg0
		    .getComponent().getParent().getLocationOnScreen().getX()
		    + x, (int) arg0.getComponent().getParent()
		    .getLocationOnScreen().getY()
		    + y);
	    popup.show();

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	    popup.hide();
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	    // TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	    // TODO Auto-generated method stub

	}

    }
}
